<?php include_once './application/views/sys_admin/common/header.php';  ?>
<div class=" definewidth m20">
		<form  class="form-inline definewidth m20">
			<button type="button" class="btn btn-success"  onclick="addRule();" >添加CPS规则</button>
	  	</form>
		<table class="table table-bordered table-hover definewidth m10">
			 <thead>
			    <tr>
			      	<td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>序号 </strong></td>
					<td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>一级百分比(%)</strong></td>
					<td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>二级百分比(%)</strong></td>
					<td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>状态</strong></td>
					<td height="35" align="center" valign="middle" bgcolor="#F1F1F1"><strong>操作 </strong></td>
			    </tr>
			   </thead>
			 <?php if($cpsrule_list){foreach ($cpsrule_list as $v){?>
			  	 <tr>
				<td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $v['id'];?></td> 
				<td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $v['first_percent'];?></td>
				<td height="35" align="center" valign="middle" bgcolor="#FFFFFF"><?php echo $v['second_percent'];?></td>
				<td height="35" align="center" valign="middle" bgcolor="#FFFFFF">
				<?php if(1==$v['fstatus']) echo "开 启"; else echo "禁 用";?>
			 	</td>
				<td height="35" align="center" valign="middle" bgcolor="#FFFFFF">
					<?php if(0==$v['fstatus']){?>
			 		<a href="javascript:;" onclick="set_cps(<?php echo  $v['id'];?>,1)">开启</a> 
			 		<?php }else{?>
			 		<a href="javascript:;" onclick="set_cps(<?php echo  $v['id'];?>,0)">禁 用</a> 
			 		<?php }?>
			 		&nbsp;&nbsp;
			 		<a  dataid="<?php echo $v['id'];?>" first_percent="<?php echo $v['first_percent'];?>" second_percent="<?php echo $v['second_percent'];?>"  fstatus="<?php echo $v['fstatus'];?>" href="javascript:;" onclick="changeRule(this);">修 改</a> 
			 		&nbsp;&nbsp;
			 		<a href="javascript:removeRule(<?php echo $v['id'];?>);">删 除</a>
			 	</td>
				</tr>	
			  <?php }}?>
			  	<tr>
			  	<td colspan="12"  class="invbd_page"><?php echo $this->pagination->create_links();?></td>
			  	</tr>
		</table>
</div>

<div id="dialog" title="系统提示" style="display:none;">
	<div id="addmsg" style="text-align:center;color:red">&nbsp;</div>
	<table class="table table-bordered table-hover m10">
		 <tr>
	        <td class="tableleft">一级百分比 </td>
	        <td><input type="text" name="first_percent" id="m_first_percent" /> </td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft">二级百分比 </td>
	        <td><input type="text" name="second_percent" id="m_second_percent" /> </td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft">启用状态 </td>
	        <td> 默认是禁用，请保存规则后再启用</td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft"></td>
	        <td> 
	        <button type="button" class="btn btn-primary"  onclick="$('#dialog').dialog('close');" >关 闭</button>
	        <button type="button" class="btn btn-success"  onclick="submitRule();" >修 改</button>
			</td>
	   	</tr>
	</table>
</div>
<div id="adddialog" title="系统提示" style="display:none;">
	<div id="addmsg" style="text-align:center;color:red">&nbsp;</div>
	<table class="table table-bordered table-hover m10">
		 <tr>
	        <td class="tableleft">一级百分比 </td>
	        <td><input type="text" name="first_percent" id="first_percent" /> </td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft">二级百分比 </td>
	        <td><input type="text" name="second_percent" id="second_percent" /> </td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft">启用状态 </td>
	        <td> 默认是禁用，请保存规则后再启用</td>
	   	</tr>
	   	 <tr>
	        <td class="tableleft"></td>
	        <td> 
	        <button type="button" class="btn btn-primary"  onclick="$('#adddialog').dialog('close');" >关 闭</button>
	        <button type="button" class="btn btn-success"  onclick="addsubmitRule();" >添加</button>
			</td>
	   	</tr>
	</table>
</div>

<script type="text/javascript">
function addRule(){
	$( "#adddialog" ).dialog({
		height:280,
		width:500,
		modal:true, 
		title:'添加CPS规则'
	});
}
function addsubmitRule(){
	if($("#first_percent").val() == ""){
		$("#adddialog" ).dialog();
		$("#addmsg").html('请输入一级推广会员所得金额百分比');
		return false;
	}
	if($("#second_percent").val() == ""){
		$("#adddialog" ).dialog();
		$("#addmsg").html('请输入二级推广会员所得金额百分比');
		return false;
	}
	$.ajax({
		type: "POST",
		url: "/sys_admin/cps/set_cps",
		data: "act=add&first_percent="+$("#first_percent").val()+"&second_percent="+$("#second_percent").val(),
		success: function(ret){
			$("#adddialog" ).dialog();
			$("#addmsg").html(ret.msg);
			setTimeout(function(){
				$("#adddialog" ).dialog("close");
			},3000);
			window.location.reload();
		},
			dataType: "json"
		});	
}

function set_cps(cps_id,flag){
	var msg = "";
	if(flag == 0){
		msg='你确定要禁用吗?';
	}else{
		msg='你确定要启用吗?';
	}
	if(confirm(msg))
	{
		if(cps_id > 0){
			$.ajax({
				type: "POST",
				url: "/sys_admin/cps/set_cps",
				data: "act=set&id="+cps_id+"&fstatus="+flag,
				success: function(ret){
					window.location.reload();
				},
				dataType: "json"
			});	
		}	
	}	
}

function submitRule(){
	var id = $("#m_id").val();
	if(id > 0){
		$.ajax({
			type: "POST",
			url: "/sys_admin/cps/set_cps",
			data: "act=edit&id="+id+"&first_percent="+$("#m_first_percent").val()+"&second_percent="+$("#m_second_percent").val(),
			success: function(ret){
				$("#dialog" ).dialog();
				$("#msg").html(ret.msg);
				setTimeout(function(){
					$("#dialog" ).dialog("close");
				},3000);
				window.location.reload();
			},
			dataType: "json"
		});	
	}
}



function changeRule(_this){
	var id = $(_this).attr("dataid");
	if(id>0){
		$( "#dialog" ).dialog({
			height:280,
			width:500,
			modal:true, //蒙层（弹出会影响页面大小）
			title:'编辑CPS规则'
		});
		$("#m_id").val(id);
		$("#m_first_percent").val($(_this).attr("first_percent"));
		$("#m_second_percent").val($(_this).attr("second_percent"));
		if($(_this).attr("fstatus") ==0){
			$("#m_fstatus1").prop("checked",true);
		}else{
			$("#m_fstatus").prop("checked",true);
		}

	}
}

function removeRule(id){
	if(confirm('你确定要删除?'))
	{
		if(id > 0){
			$.ajax({
				type: "POST",
				url: "/sys_admin/cps/set_cps",
				data: "act=delete&id="+id,
				success: function(ret){
					window.location.reload();
				},
				dataType: "json"
			});	
		}	
	}	
}

</script>
